package app

import (
	"APT/internal/library/cache"
	"APT/internal/library/contexts"
	"APT/internal/model/input/input_basics"
	"APT/internal/model/input/input_hotel"
	"APT/internal/service"
	"context"
	"github.com/gogf/gf/v2/container/gvar"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/text/gstr"
	"strings"

	"github.com/gogf/gf/v2/errors/gerror"

	"APT/api/app/basics"
)

func (c *ControllerBasics) CouponList(ctx context.Context, req *basics.CouponListReq) (res *basics.CouponListRes, err error) {
	var (
		MemberInfo = contexts.GetMemberUser(ctx)
	)
	res = new(basics.CouponListRes)
	if res.List, res.Count, err = service.BasicsCoupon().List(ctx, &input_basics.PmsCouponListInp{
		PageReq:  req.PageReq,
		State:    req.State,
		MemberId: MemberInfo.Id,
	}); err != nil {
		return
	}
	return
}

func (c *ControllerBasics) CouponOrderList(ctx context.Context, req *basics.CouponOrderListReq) (res *basics.CouponOrderListRes, err error) {
	var (
		MemberInfo        = contexts.GetMemberUser(ctx)
		PreOrderDetailVar *gvar.Var
		PreOrderDetail    *input_hotel.PreOrderDetailModel
		IsUse             []*input_basics.PmsCouponListModel
		IsNotUse          []*input_basics.PmsCouponListModel
	)
	// 读取预下单信息
	if PreOrderDetailVar, err = cache.Instance().Get(ctx, "PreOrder_"+req.PreOrderSn); err != nil || PreOrderDetailVar.IsEmpty() {
		err = gerror.New("订单不存在")
		return
	}
	PreOrderDetail = new(input_hotel.PreOrderDetailModel)
	if err = PreOrderDetailVar.Struct(&PreOrderDetail); err != nil {
		err = gerror.New("订单错误")
		return
	}
	res = new(basics.CouponOrderListRes)
	if res.List, res.Count, err = service.BasicsCoupon().List(ctx, &input_basics.PmsCouponListInp{
		PageReq:     req.PageReq,
		State:       1,
		MemberId:    MemberInfo.Id,
		Scene:       req.Scene,
		PropertyId:  PreOrderDetail.Property.Id,
		WantUseTime: gtime.Now().Format("Y-m-d H:i:s"),
		AtLeast:     PreOrderDetail.PayInfo.AllAmount,
	}); err != nil {
		return
	}
	if len(res.List) > 0 {
		for _, coupon := range res.List {
			coupon.PropertyNames = "全部物业可用"
			if !g.IsEmpty(coupon.PropertyIds) {
				coupon.PropertyNames = ""
				var propertyArr []*input_hotel.PmsPropertyAllModel
				propertyArr, err = service.HotelService().PropertyAll(ctx, &input_hotel.PmsPropertyAllInp{
					Ids: coupon.PropertyIds,
				})
				if err != nil {
					err = gerror.Wrap(err, "获取物业失败！")
					return
				}
				//if err = gvar.New(propertyArr).Struct(&coupon.Property); err != nil {
				//	return
				//}
				// 获取物业名称以逗号 分隔
				var names []string
				for _, property := range propertyArr {
					names = append(names, property.Name)
				}
				coupon.PropertyNames = gstr.Implode("，", names)
			}

			if !g.IsEmpty(coupon.StartTime) && gtime.Now().Format("y-m-d") < coupon.StartTime.Format("y-m-d") {
				IsNotUse = append(IsNotUse, coupon)
				continue
			}

			if !g.IsEmpty(coupon.EndTime) && gtime.Now().Format("y-m-d") > coupon.EndTime.Format("y-m-d") {
				IsNotUse = append(IsNotUse, coupon)
				continue
			}

			if !g.IsEmpty(coupon.UseTime) {
				IsNotUse = append(IsNotUse, coupon)
				continue
			}
			if coupon.AtLeast > PreOrderDetail.PayInfo.AllAmount {
				IsNotUse = append(IsNotUse, coupon)
				continue
			}
			if !g.IsEmpty(coupon.PropertyIds) && !gstr.InArray(strings.Split(coupon.PropertyIds, ","), gvar.New(PreOrderDetail.Property.Id).String()) {
				IsNotUse = append(IsNotUse, coupon)
				continue
			}
			coupon.Use = true
			IsUse = append(IsUse, coupon)
		}
		IsUse = append(IsUse, IsNotUse...)
		res.List = IsUse
	}
	return
}
func (c *ControllerBasics) CouponView(ctx context.Context, req *basics.CouponViewReq) (res *basics.CouponViewRes, err error) {
	res = new(basics.CouponViewRes)
	if res.PmsCouponViewModel, err = service.BasicsCoupon().View(ctx, &req.PmsCouponViewInp); err != nil {
		return
	}
	return
}
